package com.example.mylibrary.testActivity.Fingerprint;

import android.annotation.TargetApi;
import android.app.KeyguardManager;
import android.content.Context;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.widget.Toast;
import java.security.KeyStore;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class Fingerprint {
    public static final String DEFAULT_KEY_NAME = "default_key";
    public KeyStore keyStore;
    public  Cipher cipher;
    public boolean init(Context context){
        if (supportFingerprint(context)) {
            initKey();
            return initCipher();
        }
        return false;
    }
    public FingerprintDialogFragment getMfragment(Context context,Finger finger){
        return showFingerPrintDialog(context,finger);
    }
    public boolean supportFingerprint(Context context) {
        if (Build.VERSION.SDK_INT < 23) {
            Toast.makeText(context, "您的系统版本过低，不支持指纹功能", Toast.LENGTH_SHORT).show();
            return false;
        } else {
            KeyguardManager keyguardManager = context.getSystemService(KeyguardManager.class);
            FingerprintManager fingerprintManager = context.getSystemService(FingerprintManager.class);
            if (!fingerprintManager.isHardwareDetected()) {
                Toast.makeText(context, "您的手机不支持指纹功能", Toast.LENGTH_SHORT).show();
                return false;
            } else if (!keyguardManager.isKeyguardSecure()) {
                Toast.makeText(context, "您还未设置锁屏，请先设置锁屏并添加一个指纹", Toast.LENGTH_SHORT).show();
                return false;
            } else if (!fingerprintManager.hasEnrolledFingerprints()) {
                Toast.makeText(context, "您至少需要在系统设置中添加一个指纹", Toast.LENGTH_SHORT).show();
                return false;
            }
        }
        return true;
    }

    /**
     * 判断是否支持指纹
     * @param context
     * @return
     */
    public boolean inspectFingerprint(Context context) {
        if (Build.VERSION.SDK_INT < 23) {
            return false;
        } else {
            FingerprintManager fingerprintManager = context.getSystemService(FingerprintManager.class);
            if (!fingerprintManager.isHardwareDetected()) {
                return false;
            }
        }
        return true;
    }
    @TargetApi(23)
    private void initKey() {
        try {
            keyStore = KeyStore.getInstance("AndroidKeyStore");
            keyStore.load(null);
            KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
            KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(DEFAULT_KEY_NAME,
                    KeyProperties.PURPOSE_ENCRYPT |
                            KeyProperties.PURPOSE_DECRYPT)
                    .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                    .setUserAuthenticationRequired(true)
                    .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7);
            keyGenerator.init(builder.build());
            keyGenerator.generateKey();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @TargetApi(23)
    private boolean initCipher() {
        try {
            SecretKey key = (SecretKey) keyStore.getKey(DEFAULT_KEY_NAME, null);
             cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
                    + KeyProperties.BLOCK_MODE_CBC + "/"
                    + KeyProperties.ENCRYPTION_PADDING_PKCS7);
            cipher.init(Cipher.ENCRYPT_MODE, key);
//            mfragment = showFingerPrintDialog(context,finger);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    private FingerprintDialogFragment showFingerPrintDialog(Context context,Finger finger) {
        FingerprintDialogFragment fragment = new FingerprintDialogFragment();
        fragment.setCipher(cipher,context,finger);
        return fragment;
//        fragment.show(getFragmentManager(), "fingerprint");
    }
    public interface Finger{
        void success();
        void cancel();
        void failed(String s);
    }
}
